from PyQt4 import QtGui, QtCore
from ProgramTree import *
from Viewer import *
from PreferencesWindow import *

class MainWindow(QtGui.QMainWindow):
    
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.setWindowTitle("Weblessdoc")

        self.createProgramTree()
        self.createViewer()
        self.createPreferencesWindow()
        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()
        self.createConnections()
        
        self.readSettings()

    def createPreferencesWindow(self):
        self.preferencesWindow = PreferencesWindow()
        self.preferencesWindow.hide()

    def createActions(self):
        #exit
        self.exitAct = QtGui.QAction(self.tr("E&xit"), self);
        self.exitAct.setShortcut(self.tr("Ctrl+Q"));
        self.exitAct.setStatusTip(self.tr("Exit the application"));
        self.connect(self.exitAct, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("close()"));

        #preferences
        self.preferencesAct = QtGui.QAction(self.tr("Preferences"), self)
        self.preferencesAct.setStatusTip(self.tr("Show preferences window"))
        self.connect(self.preferencesAct, QtCore.SIGNAL("triggered()"), self.preferencesWindow, QtCore.SLOT("show()"))

        #about
        self.aboutAct = QtGui.QAction(self.tr("&About"), self);
        self.aboutAct.setStatusTip(self.tr("Show the application's About box"));
        self.connect(self.aboutAct, QtCore.SIGNAL("triggered()"), self.about);

        #about qt
        self.aboutQtAct = QtGui.QAction(self.tr("About &Qt"), self);
        self.aboutQtAct.setStatusTip(self.tr("Show the Qt library's About box"));
        self.connect(self.aboutQtAct, QtCore.SIGNAL("triggered()"), Qt.qApp, QtCore.SLOT("aboutQt()"));


    def createProgramTree(self):
        self.programtree = ProgramTree()
        self.programtree.updateModel()

        self.programtreedock = QtGui.QDockWidget()
        self.programtreedock.setWidget(self.programtree)
        
        self.addDockWidget(Qt.Qt.LeftDockWidgetArea, self.programtreedock)

    def createViewer(self):
        self.viewer = Viewer()
        self.setCentralWidget(self.viewer)
    
    def createConnections(self):
        self.connect(self.programtree, QtCore.SIGNAL("clicked(QModelIndex)"), self.viewer.showItemByIndex)

    def createToolBars(self):
        self.fileToolBar = self.addToolBar(self.tr("File"));
        self.editToolBar = self.addToolBar(self.tr("Edit"));
    
    def createStatusBar(self):
        self.statusBar().showMessage(self.tr("Ready"));
    
    def createMenus(self):
        menuBar = self.menuBar()
        
        self.fileMenu = menuBar.addMenu(self.tr("&File"));
        self.fileMenu.addSeparator();
        self.fileMenu.addAction(self.exitAct);
        
        self.editMenu = menuBar.addMenu(self.tr("&Edit"));
        self.editMenu.addSeparator()
        self.editMenu.addAction(self.preferencesAct)

        menuBar.addSeparator();

        self.helpMenu = menuBar.addMenu(self.tr("&Help"));
        self.helpMenu.addAction(self.aboutAct);
        self.helpMenu.addAction(self.aboutQtAct);

    def readSettings(self):
        settings = QtCore.QSettings()
        pos = settings.value("pos", QtCore.QVariant(QtCore.QPoint(200, 200))).toPoint()
        size = settings.value("size", QtCore.QVariant(QtCore.QSize(400, 400))).toSize()
        self.resize(size)
        self.move(pos)

    def writeSettings(self):
        settings = QtCore.QSettings()
        settings.setValue("pos", QtCore.QVariant(self.pos()))
        settings.setValue("size", QtCore.QVariant(self.size()))

    def closeEvent(self, event):
        self.writeSettings()
        event.accept()

    def about(self):
        QtGui.QMessageBox.about(self, self.tr("About Weblessdoc"), self.tr("About."))